<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script src="./lib/vue.js"></script>
</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./lib/vue.js"></script>
  <style>
    .error {
      color: #f00;
    }
  </style>
</head>

<body>
  <div id="app">
    <input type="text" v-model="title">
    <span :class="{error:msg!='输入合法的用户名'?true:false}">{{msg}}</span>
    <hr>
    <input type="text" v-model="user.name">

    <hr>
    <button :disabled="disabled">登录</button>
  </div>

  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        disabled: false,
        title: '',
        msg: '输入合法的用户名',
        user: {
          id: 1,
          name: '张三'
        }
      },
      watch: {
        title(newV, oldV) {
          // console.log(newV, oldV)
          if ('admin' === newV) {
            this.disabled = true;
            this.msg = '此用户名是保留的用户名，不让使用'
          } else {
            this.disabled = false;
            this.msg = '输入合法的用户名'
          }
        },
        // 对于整个对象来监听，不太建议去用
        /* user: {
          // 对引用类型进行侦听
          deep: true,
          handler(n, o) {
            console.log(n)
          }
        } */
        'user.name'(n, o) {
          console.log(n)
        }
      }
    })
  </script>
</body>

</html>